package com.gulimall.cart.vo;

import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author kkk
 * @date 2022/8/2
 */
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class CartVo {
    /**
     * 购物车商品集合
     */
    private List<CartItemVo> items;

    /**
     * 购物车商品总数量
     */
    private Integer totalCount;
    /**
     * 购物车商品类型数量
     */
    private Integer countType;

    /**
     * 购物车商品总价
     */
    private BigDecimal totalAmount = new BigDecimal("0");

    /**
     * 减免价格
     */
    private BigDecimal reduce = new BigDecimal("0.00");

    public void setItems(List<CartItemVo> items) {
        this.items = items;
    }

    public List<CartItemVo> getItems() {
        return items;
    }

    public Integer getTotalCount() {
        Integer count = 0;
        for (CartItemVo item : items) {
            count += item.getCount();
        }
        return count;
    }

    public Integer getCountType() {
        Integer count = 0;
        for (CartItemVo item : items) {
            count++;
        }
        return count;
    }

    public BigDecimal getTotalAmount() {
        BigDecimal amount = new BigDecimal("0");
        for (CartItemVo item : items) {
            if (item.check) {
                amount = amount.add(item.getTotalPrice());
            }
        }
        return amount.subtract(reduce);
    }

    public void setReduce(BigDecimal reduce) {
        this.reduce = reduce;
    }

    public BigDecimal getReduce() {
        return reduce;
    }

    @NoArgsConstructor
    @AllArgsConstructor
    @ToString
    public static class CartItemVo {
        private Long skuId;
        private boolean check = true;
        private String title;
        private String image;
        private List<String> skuAttr;
        private BigDecimal price = new BigDecimal("0");
        private Integer count;
        private BigDecimal totalPrice = new BigDecimal("0");

        public Long getSkuId() {
            return skuId;
        }

        public void setSkuId(Long skuId) {
            this.skuId = skuId;
        }

        public boolean isCheck() {
            return check;
        }

        public void setCheck(boolean check) {
            this.check = check;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getImage() {
            return image;
        }

        public void setImage(String image) {
            this.image = image;
        }

        public List<String> getSkuAttr() {
            return skuAttr;
        }

        public void setSkuAttr(List<String> skuAttr) {
            this.skuAttr = skuAttr;
        }

        public BigDecimal getPrice() {
            return price;
        }

        public void setPrice(BigDecimal price) {
            this.price = price;
        }

        public Integer getCount() {
            return count;
        }

        public void setCount(Integer count) {
            this.count = count;
        }

        public BigDecimal getTotalPrice() {
            return price.multiply(new BigDecimal(count));
        }
    }
}
